package com.qf.dao.daoimpl;

import com.alibaba.druid.pool.DruidDataSource;
import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.util.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    DruidDataSource dateSource = DBUtil.getDateSource();
    QueryRunner qr = new QueryRunner(dateSource);

    @Override
    public User FindUserByName(String username) {
        String sql = "select * from tb_user where username = ?";
        try {
            User user = qr.query(sql, new BeanHandler<User>(User.class), username);
            return  user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into tb_user (username , password , email , gender , status , role , code) value(?,?,?,?,?,?,?)";
        try {
           int count = qr.update(sql,user.getUsername(),user.getPassword(),
                    user.getEmail(),user.getGender(),user.getStatus(),user.getRole(),
                   user.getCode()
            );
           return  count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
